package com.pcitc.app.dao.hibernate;



import java.util.List;

import org.hibernate.LockMode;

import com.pcitc.app.dao.TblOrgDao;
import com.pcitc.app.model.Operator;
import com.pcitc.app.model.TblOrg;
import com.pcitc.app.model.TblPosition;
import com.pcitc.app.model.TblUser;


public class TblOrgDaoHibernate extends  GenericDaoHibernate<TblOrg, Long>implements TblOrgDao{
	
	public TblOrgDaoHibernate(){
		super(TblOrg.class);
	}
	/** 
     *获取组织机构列表
    */
	public List<TblOrg> getOrgList(){
		//System.out.println("@@@@@ query tblorg list @@@@");
		//log.debug("finding org list!");
		List<TblOrg> list = this.getHibernateTemplate().find("from TblOrg o order by upper(o.orgname)");
		return list;
	}
	
	
	public List<TblOrg> findByProperty(String propertyName, Object value) {
		log.debug("finding Org instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TblOrg as org where org."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	/** 
     * 根据组织机构名查找组织机构
    */
	public  TblOrg loadOrgByOrgname(String orgname){
		 List<TblOrg> orgs=getHibernateTemplate().find("from TblOrg as org where org.orgname=?", orgname);
		if(orgs == null || orgs.size() == 0){
			return null;
		}
		else{
			return (TblOrg)orgs.get(0);
		}
	}
	
	public List<TblOrg> findByExample(TblOrg tblOrg) {
		
		try {
			List<TblOrg> results = getHibernateTemplate().findByExample(tblOrg);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	/** 
     *  根据组织机构ID查找组织机构
    */

    public TblOrg loadOrgByOrgid(Long orgid){
    	return (TblOrg)getHibernateTemplate().get(TblOrg.class, orgid);
    }
    /** 
     * 保存组织机构
    */
    public TblOrg saveOrg(TblOrg tblOrg){
    	 log.debug("org's id: " + tblOrg.getOrgid());
         try {
         	getHibernateTemplate().saveOrUpdate(tblOrg);
         	log.debug("save successful");
             // necessary to throw a DataIntegrityViolation and catch it in UserManager
             getHibernateTemplate().flush();
             return tblOrg;
 		} catch (RuntimeException re) {
 			log.error("save failed", re);
 			throw re;
 		}
    }
    /** 
     * 更新组织机构
    */
 	public TblOrg updateOrg(TblOrg tblOrg){
 	    	 log.debug("org's id: " + tblOrg.getOrgid());
 	         try {
 	         	getHibernateTemplate().update(tblOrg);
 	         	log.debug("update successful");
 	             // necessary to throw a DataIntegrityViolation and catch it in UserManager
 	             getHibernateTemplate().flush();
 	             return tblOrg;
 	 		} catch (RuntimeException re) {
 	 			log.error("update failed", re);
 	 			throw re;
 	 		}
    	
    }
 	/** 
     * 删除组织机构
    */
    public void removeOrg(TblOrg tblOrg){
    	log.debug("org's id: " + tblOrg.getOrgid());
    	try {
    		getHibernateTemplate().delete(tblOrg);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
    	
    }
    /** 
     * 根据组织机构ID进行删除
    */
    public void removeOrgByid(long orgid){
    	try{
    	  TblOrg tblOrg = this.loadOrgByOrgid(orgid);
	      getHibernateTemplate().delete(tblOrg);
	      log.debug("delete successful");
    	}catch(RuntimeException re){
    		log.error("delete failed", re);
    		throw re;
    	}
    	
    }
    /** 
     * 删除所有组织机构
    */
    public void removeAll(List<TblOrg> tblOrgs){
    	try {
    		getHibernateTemplate().deleteAll(tblOrgs);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
    	
    }
    public TblOrg merge(TblOrg detachedInstance) {
		try {
			TblOrg result = (TblOrg) getHibernateTemplate().merge(
					detachedInstance);
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachDirty(TblOrg instance) {
		try {
			getHibernateTemplate().saveOrUpdate(instance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachClean(TblOrg instance) {
		try {
			getHibernateTemplate().lock(instance,LockMode.NONE);
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
    /** 
     * 根据组织机构ID查找属于该组织机构的岗位列表
    */
    public List<TblPosition> getPositionListByOrgId(long orgid){
    	try{
    		List<TblPosition> positions=getHibernateTemplate().find("from TblPosition position where position.orgid=?",orgid);	
    		log.debug("find positions successful, positions' size:"+positions.size());
    		return positions;
    	}catch(RuntimeException re){
    		log.error("find  failed", re);
    		throw re;
    	}
    }
}
